package com.hpxx.ylzswl.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;
import android.widget.TextView;

import com.hpxx.ylzswl.R;
import com.hpxx.ylzswl.base.BaseActivity;
import com.hpxx.ylzswl.event.PhoneEvent;
import com.hpxx.ylzswl.https.HttpAddress;
import com.hpxx.ylzswl.https.RequestParams;
import com.hpxx.ylzswl.utils.ConstantsUtils;
import com.universal.frame.generalutils.Constants;
import com.universal.frame.generalutils.GeneralUtil;
import com.universal.frame.generalutils.JsonUtil;
import com.universal.frame.generalutils.SharedPreferencesUtil;
import com.universal.frame.generalutils.ToastUtil;
import com.universal.frame.generalutils.timer.TimerCount;
import com.universal.frame.okhttp.OKHttpUtils;
import com.universal.frame.okhttp.ResultCallBack;

import java.io.IOException;

import de.greenrobot.event.EventBus;
import okhttp3.Request;

public class UpDataPhoneActivity extends BaseActivity implements ResultCallBack,View.OnClickListener {

	private TextView mTextViewCode;

	private EditText mEditTextPhone; //电话

	private EditText mEditTextCode; //验证码

	private TextView btn_updata;
	
	private String phone;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_updata_phone);
		
		back();
		setTitle("修改手机号");
	}

	@Override
	public void initView() {
		mTextViewCode = (TextView) findViewById(R.id.up_cm_code);
		mEditTextPhone = (EditText) findViewById(R.id.et_up_phone);
		mEditTextCode = (EditText) findViewById(R.id.et_input_code);
		btn_updata = (TextView) findViewById(R.id.btn_updata);
		btn_updata.setOnClickListener(this);
		mTextViewCode.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.up_cm_code:
				getCodePost();
				break;

			case R.id.btn_updata:
				registPost();
				break;

			default:
				break;
		}
	}

	
	private void getCodePost() {

		phone = mEditTextPhone.getText().toString().trim();
		if(TextUtils.isEmpty(phone)){
			ToastUtil.showToast(this, "请输入您的手机号");
			return;
		}

		if (!phone.matches(Constants.PHONE_ZZ)) {
			ToastUtil.showToast(this, "请输入有效的手机号");
			return;
		}

		OKHttpUtils.postAsync(
				this,
				HttpAddress.SERVICE_REGISTER_CODE,
				new RequestParams(this).getCodeParams(phone), this,
				true, 1);
	}
	
	/**
	 * 注册请求
	 */
	private void registPost() {
		phone = mEditTextPhone.getText().toString().trim();
		String code = mEditTextCode.getText().toString().trim();
		
		if(TextUtils.isEmpty(phone)){
			ToastUtil.showToast(this, "请输入您的手机号");
			return;
		}
		
		if (!phone.matches(Constants.PHONE_ZZ)) {
			ToastUtil.showToast(this, "请输入有效的手机号");
			return;
		}
		
		
		if(TextUtils.isEmpty(code)){
			ToastUtil.showToast(this, "请输入验证码");
			return;
		}

		OKHttpUtils.postAsync(this
				, HttpAddress.UPDATE_PHONE_URL
				, new RequestParams(this).getUpdatePhoneParams(phone, code,3), this
				,true,2);
	}

	@Override
	public void onSuccess(String response, int flag) {
		try {
			String status = JsonUtil.getString(response, "status", "");
			switch (flag) {
			case 1:
				if("1".equals(status)){
					countdown();
				}else{
					String msg = JsonUtil.getString(response, "msg", "");
					ToastUtil.showToast(this, msg);
				}
				break;
				
			case 2:
				if("1".equals(status)){
					String msg = JsonUtil.getString(response, "msg", "");
					ToastUtil.showToast(this, msg);
					SharedPreferencesUtil.putString(getApplicationContext(), ConstantsUtils.USER_PHONE,phone);
					EventBus.getDefault().post(new PhoneEvent(phone));
					finish();
				}else{
					String msg = JsonUtil.getString(response, "msg", "");
					ToastUtil.showToast(this, msg);
				}
				
				break;

			default:
				break;
			}
		} catch (Exception e) {
			GeneralUtil.tryShow(this, e);
		}
	}

	@Override
	public void onFailure(Request request, IOException e, int flag) {
		
	}
	
	
	/**
	 * 获取验证码
	 */
	private void countdown() {
		TranslateAnimation amin = new TranslateAnimation(mEditTextPhone.getWidth(),
				mEditTextPhone.getWidth() + 10, mEditTextPhone.getHeight(),
				mEditTextPhone.getHeight());
		if (TextUtils.isEmpty(phone)) {
			ToastUtil.showToast(this, "空了");
			amin.setInterpolator(new CycleInterpolator(2f));
			amin.setDuration(300);
			amin.start();
			mEditTextPhone.setAnimation(amin);
			return;
		}
		TimerCount timer = new TimerCount(60000, 1000, mTextViewCode);
		timer.start();
	}


}
